package com.sgb.jicai.viewmodel;

import androidx.lifecycle.ViewModel;

import com.im.immodel.IMUtils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.sgb.jicai.R;
import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.jicai.entity.JoinJicaiDetailEntity;
import com.sgb.jicai.entity.SubmitJoininJicaiEntity;
import com.sgb.jicai.view.activity.JoinInJiCaiActivity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.GlideEngine;
import com.swgk.core.util.MToast;
import com.swgk.core.view.areapopup.AddressEntity;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class JoinInJiCaiModel extends ViewModel {

    private JoinInJiCaiActivity activity;

    // 获取集采单据详情
    public void getDetailData(String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getOrderDetail(no).enqueue(new ICallBack<BaseEntity<JoinJicaiDetailEntity>>() {
            @Override
            public void onSuccess(BaseEntity<JoinJicaiDetailEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() == null || data.getState() == null) {
                    MToast.showToast(activity, "获取单据详情失败");
                    return;
                }
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    activity.updateView(data.getData());
                } else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

    //提交加盟集采信息
    public void submitDetailData(SubmitJoininJicaiEntity entity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().addParticipant(entity).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    MToast.showToast(activity, "提交信息成功");
                    activity.submitSucceed();
                } else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

    public void findCustomerServiceImAccount(String customerServiceContactsId) {
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map map = new HashMap();
        map.put("customerServiceContactsId", customerServiceContactsId);
        JiCaiBaseApi.getApi().getfindCustomerServiceImAccount(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() == null) return;
                if (!data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    MToast.showToast(activity, data.getMsg());
                } else {
                    String dataIMid = data.getData().toString();
                    IMUtils.getInstance().buildSingleTalking(dataIMid);
//                    NimUIKit.startP2PSession(activity, dataIMid);
                }
            }
        });
    }

    //获取省市区
    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK))
                    activity.initAddressData(data.getData());
            }
        });
    }

    //获取上一次加盟的信息
    public void getJicaiJoinDetailDatas(String participantNo) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getParticipantDetail(participantNo).enqueue(new ICallBack<BaseEntity<JoinJicaiDetailEntity>>() {
            @Override
            public void onSuccess(BaseEntity<JoinJicaiDetailEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() == null || data.getState() == null) {
                    MToast.showToast(activity, "获取单据详情失败");
                    return;
                }
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    activity.updateView(data.getData());
                } else {
                }
            }
        });
    }
    public void pictureSelector(int number) {
        PictureSelector.create(activity)
                .openGallery(PictureMimeType.ofImage())
                .loadImageEngine(GlideEngine.createGlideEngine())
                .theme(R.style.picture_default_style)
                .selectionMode(PictureConfig.MULTIPLE)
                .compress(true)//是否压缩
                .minimumCompressSize(10240)//小于10M不压缩
                .maxSelectNum(number)//判断还能选多少张图片
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }
    //上传附件
    public void uploadAnnexFileModel(File file, int completeNum) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + completeNum + "image.jpg\"", requestBody);
        DialogHelper.showProgressDialog(activity, null, "上传中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().uploadFile(photos).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    MToast.showToast(activity, "上传失败！");
                    return;
                }
                if (response.body().getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    activity.uploadResult(response.body().getData().toString());
                } else {
                    MToast.showToast(activity, "上传失败！");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, "上传失败！" + t.getMessage());
            }
        });
    }

    public void setActivity(JoinInJiCaiActivity joinInJiCaiActivity) {
        this.activity = joinInJiCaiActivity;
    }
}